<template>
	<view>
		<view class="title_box">
			注册账户
		</view>
		<u-form labelPosition="top" :model="form" :rules="rules" ref="form">
			<view class="card_box">
				<u-form-item prop="account" label="手机号">
					<u-input v-model="form.account" type="number" maxlength='11' placeholder="请输入手机号"
						:placeholder-style="placeholderStyle"></u-input>
				</u-form-item>
			</view>
			<view class="card_box">
				<view class="card_box-title">验证码</view>
				<u-form-item prop="code">
					<u-input v-model="form.code" placeholder="请输入验证码" :placeholder-style="placeholderStyle"></u-input>
					<span slot="right" class="code-title" @click="getCode">{{codeTips}}</span>
				</u-form-item>
			</view>
			<u-verification-code seconds="60" ref="uCode" @change="codeChange"></u-verification-code>
			<view class="card_box" v-if="passwordShow">
				<view class="card_box-title">密码</view>
				<u-form-item prop="password">
					<u-input v-model="form.password" type="password" placeholder="请输入密码"
						:placeholder-style="placeholderStyle" />
				</u-form-item>
			</view>
		</u-form>
		<view class="card_box card_text_login" @click="login()">
			同意协议并注册
		</view>
		<view class="card_box m-a-t color-c7">我已阅读并同意：<span style="color:#014AF8" @click="navigateTo('/pages/register/agreement')">旌信服软件服务协议</span></view>
	</view>
</template>

<script>
	import * as auth from '@/api/auth'
	export default {
		data() {
			return {
				placeholderStyle: 'color:#aaaaaa',
				codeTips: '',
				passwordShow: false,
				form: {
					account: '',
					password: '',
					code: ''
				},
				rules: {
					account: [{
						required: true,
						message: '请输入手机号',
						trigger: 'blur',
					}],
					code: [{
						required: true,
						message: '请输入验证码',
						trigger: 'blur',
					}],
					password: [{
						required: true,
						message: '请输入密码',
						trigger: 'blur',
					}],
				}
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules);
		},
		methods: {
			login() {
				this.$refs.form.validate(valid => {
					if (valid) {
						uni.showLoading({
							title: '正在登录...'
						});
						auth.login(this.form).then(res => {
							uni.hideLoading();
							uni.setStorageSync('userInfo', JSON.stringify(res.data))
							this.showToast('登录成功', 'success')
							this.reLaunch('/pages/index/index')
						})
					} else {
						return 
					}
				})
			},
			// 获取验证码
			getCode() {
				this.passwordShow = true
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码',
						mask: true
					})
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						this.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 2000);
				} else {
					this.$u.toast('倒计时结束后再发送');
				}
			},
			codeChange(text) {
				this.codeTips = text;
			},
		}
	}
</script>

<style>
	.title_box {
		font-size: 42rpx;
		margin-top: 24rpx;
		margin-left: 60rpx;
		font-weight: bold;
		/* padding: 140rpx 0 60rpx 0; */
	}

	.card_box {
		margin: 0 60rpx;
		padding: 10rpx 30rpx;
	}

	.card_box-title {
		font-size: 28rpx;
		color: #000000;
	}

	/* .card_box+.card_box {
		margin-top: 36rpx;
	} */

	.card_text_login {
		background-color: #014AF8;
		color: #FFFFFF;
		font-size: 30rpx;
		text-align: center;
		padding-top: 22rpx;
		padding-bottom: 22rpx;
		margin-top: 18rpx;
		border-radius: 8rpx;
	}

	.car {
		color: #014AF8;
		margin: 30rpx 66rpx;
	}

	.ma-l-a {
		margin-left: auto;
	}

	.bottom_box {
		margin: 0 36rpx;
		text-align: center;
	}

	.m-a-t {
		margin-top: 60rpx;
	}

	.color-c7 {
		color: #C7C7CC;
	}

	.code-title {
		color: #014AF8;
	}
</style>
